
class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        ListNode ret=new ListNode(-1);
         ListNode retu=ret;
        while(list1!=null&&list2!=null){
            if(list1.val<=list2.val){
                ret.next=list1;
                list1=list1.next;
                ret=ret.next;
            }else{
                 ret.next=list2;
                list2=list2.next;
                ret=ret.next;
            }
        }
        if(list1==null){
            ret.next=list2;
        }else{
            ret.next=list1;
        }
        return retu.next;
    }
}

import java.util.ArrayList;
public class Solution {
    //两个链表合并函数
    public ListNode Merge(ListNode list1, ListNode list2) { 
        //一个已经为空了，直接返回另一个
        if(list1 == null) 
            return list2;
        if(list2 == null)
            return list1;
        //加一个表头
        ListNode head = new ListNode(0); 
        ListNode cur = head;
        //两个链表都要不为空
        while(list1 != null && list2 != null){ 
            //取较小值的节点
            if(list1.val <= list2.val){ 
                cur.next = list1;
                //只移动取值的指针
                list1 = list1.next; 
            }else{
                cur.next = list2;
                //只移动取值的指针
                list2 = list2.next; 
            }
            //指针后移
            cur = cur.next; 
        }
        //哪个链表还有剩，直接连在后面
        if(list1 != null) 
            cur.next = list1;
        else
            cur.next = list2;
        //返回值去掉表头
        return head.next; 
    }
    
    //划分合并区间函数
    ListNode divideMerge(ArrayList<ListNode> lists, int left, int right){ 
        if(left > right) 
            return null;
        //中间一个的情况
        else if(left == right) 
            return lists.get(left);
        //从中间分成两段，再将合并好的两段合并
        int mid = (left + right) / 2; 
        return Merge(divideMerge(lists, left, mid), divideMerge(lists, mid + 1, right));
    }
    
    public ListNode mergeKLists(ArrayList<ListNode> lists) {
        //k个链表归并排序
        return divideMerge(lists, 0, lists.size() - 1);
    }
}

// bm14
import java.util.*;
public class Solution {
    public ListNode oddEvenList (ListNode head) {
        //如果链表为空，不用重排
        if(head == null) 
            return head;
        //even开头指向第二个节点，可能为空
        ListNode even = head.next; 
        //odd开头指向第一个节点
        ListNode odd = head; 
        //指向even开头
        ListNode evenhead = even; 
        while(even != null && even.next != null){
            //odd连接even的后一个，即奇数位
            odd.next = even.next; 
            //odd进入后一个奇数位
            odd = odd.next; 
            //even连接后一个奇数的后一位，即偶数位
            even.next = odd.next; 
            //even进入后一个偶数位
            even = even.next; 
        } 
        //even整体接在odd后面
        odd.next = evenhead; 
        return head;
    }
}
bm15🐂
import java.util.*;
public class Solution {
    public ListNode deleteDuplicates (ListNode head) {
        //空链表
        if(head == null) 
            return null;
        //遍历指针
        ListNode cur = head; 
        //指针当前和下一位不为空
        while(cur != null && cur.next != null){ 
            //如果当前与下一位相等则忽略下一位
            if(cur.val == cur.next.val) 
                cur.next = cur.next.next;
            //否则指针正常遍历
            else 
                cur = cur.next;
        }
        return head;
    }
}
*******************
import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode deleteDuplicates (ListNode head) {
        if(head==null){
            return head;
        }
        // write code here
        ListNode m=head;
        ListNode n=head.next;
        while(n!=null){
            while(m.val==n.val){
                if(n.next==null){
                    m.next=null;
                    return head;
                }
                n=n.next;
            }
            m.=n;
            n=n.next;
        }
        return head;
    }
}

bm16
import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode deleteDuplicates (ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        // write code here
        ListNode pre=new ListNode(-1);
        ListNode ret=pre;
        pre.next=head;
         ListNode cur=head;
        while(cur!=null){
            ListNode tmp=cur.next;
            while(tmp.val==cur.val){
               tmp=tmp.next;
            }
            pre.next=tmp;
            cur=tmp;
        }
        return ret.next;
    }
}

bm16🐂
import java.util.*;
public class Solution {
    public ListNode deleteDuplicates (ListNode head) {
        //空链表
        if(head == null) 
            return null;
        ListNode res = new ListNode(0);
        //在链表前加一个表头
        res.next = head; 
        ListNode cur = res;
        while(cur.next != null && cur.next.next != null){ 
            //遇到相邻两个节点值相同
            if(cur.next.val == cur.next.next.val){ 
                int temp = cur.next.val;
                //将所有相同的都跳过
                while (cur.next != null && cur.next.val == temp) 
                    cur.next = cur.next.next;
            }
            else 
                cur = cur.next;
        }
        //返回时去掉表头
        return res.next; 
    }
}
